package com.core.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.core.sys.vo.ActiveUserVo;



public class LoginInterceptor extends HandlerInterceptorAdapter {

	//在执行handler之前来执行的
	//用于用户认证校验、用户权限校验
	@Override
	public boolean preHandle(HttpServletRequest request,
		HttpServletResponse response, Object handler) throws Exception {
		
		HttpSession session = request.getSession();
		ActiveUserVo acticeUser = (ActiveUserVo) session.getAttribute("activeUser");
		if(acticeUser == null ){
			response.sendRedirect(request.getContextPath() + "/login");
			return false;
		}
//		ActiveUserVo acticeUser = new ActiveUserVo();
//		acticeUser.setId(1);
//        acticeUser.setUserName("AA");    
//        session.setAttribute("activeUser", acticeUser);
        
		return super.preHandle(request, response, handler);
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}

	@Override
	public void afterConcurrentHandlingStarted(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		super.afterConcurrentHandlingStarted(request, response, handler);
	}
	
}
